<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>斐波那契数列的闭包缓存优化</title>
</head>
<body>
    <script>
        // 浏览器计算崩溃
        // function fib(n) {
        //     if(n ===0 || n === 1) {
        //         return n
        //     }else {
        //         return fib(n-1) + fib(n-2)
        //     }
        // }
        // const r = fib(50)
        // console.log(r) 
    </script>
    <script>
        const fib2 = (function() {
            const arr = []
            return function(n) {
                if(n<=1) {
                    return n
                }else {
                    arr[n-1] = arr[n-1] || fib2(n-1)
                    arr[n-2] = arr[n-2] || fib2(n-2)
                    return arr[n-1] + arr[n-2]
                }
            }
        })()
        const r = fib2(50)
        console.log(r)
    </script>
</body>
</html>